package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.QuestionDao;
import com.itheima.domain.Question;
import com.itheima.service.QuestionService;
import com.itheima.utils.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class QuestionServiceImpl implements QuestionService {
    @Override
    public PageInfo<Question> findByPage(int currentPage, int pageSize) {
        PageInfo<Question> page = null;
        SqlSession sqlSession = null;
        try {
            //1 通过MybatisUtil工厂类获取SqlSession对象
            sqlSession = MybatisUtil.getSqlSession();
            //2 通过MybatisUtil工厂类获取dao接口的代理对象
            QuestionDao mapper = MybatisUtil.getMapper(sqlSession, QuestionDao.class);
            //3 执行操作释放资源
            //3.1 在查询所有之前设置分页参数
            PageHelper.startPage(currentPage,pageSize);
            List<Question> list = mapper.findAll();
             page = new PageInfo<>(list);
            //MybatisUtil.commit(sqlSession);
        } finally {
            MybatisUtil.close(sqlSession);
        }
        return page;
    }

    @Override
    public void save(Question question) {
        SqlSession sqlSession=null;
        try {
             sqlSession = MybatisUtil.getSqlSession();
            QuestionDao mapper = MybatisUtil.getMapper(sqlSession, QuestionDao.class);
            //生成一个唯一的id，保存到question
            String id = UUID.randomUUID().toString().replace("-", "");
            question.setId(id);
            mapper.save(question);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public Question findById(String id) {
        Question question = null;
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            QuestionDao mapper = MybatisUtil.getMapper(sqlSession, QuestionDao.class);

            question =  mapper.findById(id);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
        return question;
    }

    @Override
    public void update(Question question) {
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            QuestionDao mapper = MybatisUtil.getMapper(sqlSession, QuestionDao.class);

            mapper.update(question);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            QuestionDao mapper = MybatisUtil.getMapper(sqlSession, QuestionDao.class);

            Stream.of(ids).forEach(mapper::delete);
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
    }

    @Override
    public List<Question> findAll() {
        List<Question>  list = null;
        SqlSession sqlSession=null;
        try {
            sqlSession = MybatisUtil.getSqlSession();
            QuestionDao mapper = MybatisUtil.getMapper(sqlSession, QuestionDao.class);

            list =  mapper.findAll();
            MybatisUtil.commit(sqlSession);

        } finally {
            MybatisUtil.close(sqlSession);
        }
        return list;
    }

    @Override
    public void toExport(ServletOutputStream outputStream) throws IOException {
        //使用类加载器获得内存中工作簿的位置
        String path = this.getClass().getClassLoader().getResource("question_template.xlsx").getPath();
        //1.创建一个work工作簿读取上一步获得到的工作簿
        XSSFWorkbook workbook = new XSSFWorkbook(path);
        //2.获取其中的sheet表，这里只有一个
        XSSFSheet sheet = workbook.getSheetAt(0);
        //3。调用dao中的findAll方法，获得问题的集合
        List<Question> list = findAll();
        //4.将返回的Question类型集合转换成String以遍历
        List<String[]> questions = list.stream().map(question -> question.toString().split(",")).collect(Collectors.toList());
        //5.创建row和cell把集合中数据写道其中
        //定义通用的样式
        CellStyle style = workbook.createCellStyle();
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);
        int initRow = 3;
        for (String[] files : questions) {
//            把一整张表对象遍历成一行一行的
            XSSFRow row = sheet.createRow(initRow++);
            int initcell = 1;
            for (String file : files) {
//                每多一行就多若干个单元格
                XSSFCell cell = row.createCell(initcell++);
                //向单元格中写入数据
                cell.setCellValue(file);
                cell.setCellStyle(style);
            }
        }

        //6.将写完成后的数据输出
        workbook.write(outputStream);
        //7.释放资源
        workbook.close();
    }
}
